package se.ytterman.jserial.tests;

import java.lang.*;
import java.util.*;
import java.io.*;

import se.ytterman.jserial.jni.SerialInterfaceFactory;
import se.ytterman.jserial.jni.SerialInterface;

import se.ytterman.jserial.util.BinaryDumpUtil;

public class BasicTest {

    public static void main(String argv[]) {
	// Check if we have a proper number of arguments...
	if(argv.length == 0) {
	    System.out.println("No testcase name givven, terminating");
	}
	else {
	    // Define different tests...
	    if(argv[0].compareTo("dummy") == 0) {
		// Test 0, dummy test
		System.out.printf("-----------------------------------------------------\n");
		System.out.println("Dummy test...");	    

	    } else if(argv[0].compareTo("list_interfaces") == 0) {
		// Test 1, list interfaces
		System.out.printf("-----------------------------------------------------\n");
		System.out.println("Listing insterfaces...");

		// Create Pcap interface factory 
		SerialInterfaceFactory factory = new SerialInterfaceFactory();
		
		// Fetch interface array
		SerialInterface interfaces[] = factory.getInterfaceArray();
		
		// Print interfaces...
		for(int i = 0; i < interfaces.length; i++)
		   System.out.println(interfaces[i]);
	    } else if(argv[0].compareTo("at_echo") == 0) {
		// Test 2, try AT command echo, 9600 baud, 8N1
		System.out.printf("-----------------------------------------------------\n");
		System.out.println("Testing AT command...");

		// Create Pcap interface factory 
		SerialInterfaceFactory factory = new SerialInterfaceFactory();
		
		// Fetch interface array
		SerialInterface interfaces[] = factory.getInterfaceArray();
		
		System.out.println("Sending command...");

		// Send AT\n
		String at_command = "AT\r\n";
		interfaces[0].write(at_command.getBytes());

		System.out.println("Sent: " + at_command);
		BinaryDumpUtil.printHexdump(at_command.getBytes(), System.out);

		System.out.println("Waiting for response...");
		
		// Receive OK(?)
		byte[] buffer = interfaces[0].read();
		String result = new String(buffer);
		
		System.out.println("Received: " + result);
		BinaryDumpUtil.printHexdump(buffer, System.out);
	    }
	}
    }
}
